package blackberry.util.field;

import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.component.ObjectChoiceField;

public class DjChoiceField extends DjPreparedLabelManager {

	private CustomChoiceField _choiceField;
	
	public DjChoiceField(DjLabelField itemLabel, DjLabelField infoLabel, Object[] choices) {
		this(itemLabel, infoLabel, choices, DEFAULT_WIDTH);
	}

	public DjChoiceField(DjLabelField itemLabel, DjLabelField infoLabel, Object[] choices, 
			int width) {
		super(itemLabel, infoLabel, width);
		if (choices != null) {
			_choiceField = new CustomChoiceField(choices);
		} else { 
			_choiceField = new CustomChoiceField(new String[] {""});
		}
		add(_choiceField);
	}

	/**
	 * @원리 height = choiceField.height + infoLable.height + 밑쪽 padding
	 *       (1)윗쪽 padding 은 choiceField 로 대체함
	 *       (2)infoLable.height은 전체 높이가 필요하므로 getHeight()를 사용한다.
	 */
	public int getPreferredHeight() {
		return getPreferredHeightOfChild(_choiceField) 
				+ _infoLabel.getHeight()
				+ DjFieldInfo.DEFAULT_PADDING_SIZE_px;
	}

	protected void sublayout(int width, int height) {
		
		setExtent(getPreferredWidth(), getPreferredHeight());
		
		setPositionChild(_itemLabel, 
				0, 
				DjFieldInfo.DEFAULT_PADDING_SIZE_px);
		layoutChild(_itemLabel, 
				getPreferredWidthOfChild(_itemLabel), 
				getPreferredHeightOfChild(_itemLabel));
	
		setPositionChild(_choiceField, 
				getPreferredWidth() - _choiceField.getPreferredWidth(), 
				0);
		layoutChild(_choiceField, 
				getPreferredWidthOfChild(_choiceField), 
				getPreferredHeightOfChild(_choiceField));
		
		setPositionChild(_infoLabel, 
				0, 
				_choiceField.getPreferredHeight());
		layoutChild(_infoLabel, 
				_infoLabel.getPreferredWidth(), 
				_infoLabel.getPreferredHeight());
	}
	
	
	/**
	 * manager 를 클릭해도 choice 필드가 펼쳐지도록 재정의함 
	 */
	protected boolean touchEvent(TouchEvent message) {
		if (message.getEvent() == TouchEvent.CLICK) {
			_choiceField.invokeAction(ACTION_INVOKE);
		}
		return super.touchEvent(message);
	}
	
	/**
	 * invoceAction 을 사용할 수 있게한 choice field 클래스
	 */
	private class CustomChoiceField extends ObjectChoiceField {

		public CustomChoiceField(Object[] choices) {
			super("", choices);
		}
		
		//가시성을 위함
		protected boolean invokeAction(int action) {
			return super.invokeAction(action);
		}
	}
	
	/**
	 * 초이스필드의 대리 메서드들
	 */
	public void setChoices(Object[] choices) {
		_choiceField.setChoices(choices);
	}
	
	public Object getChoice(int index) {
		return _choiceField.getChoice(index);
	}
	
	public void setFont(Font font) {
		_choiceField.setFont(font);
	}

	public void setSelectedIndex(int index) {
		_choiceField.setSelectedIndex(index);
	}
	
	public void setSelectedIndex(Object object) {
		_choiceField.setSelectedIndex(object);
	}
	
	public int getSelectedIndex() {
		return _choiceField.getSelectedIndex();
	}
	
	public void setChangeListener(FieldChangeListener arg0) {
		_choiceField.setChangeListener(arg0);
	}
}